using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;

namespace AliCMS.SystemModule.Bll
{
    public class Log : DbObjectBase, ILog
    {
		public Log(int instanceID, string lang, bool checkRight, string database)
			: base(instanceID, lang, checkRight, database)
		{
		}

		public override object ExecuteService(HttpContext context)
		{
            object result = null;

            string operation = context.Request.QueryString["Operation"];
			if (operation == "Add")
			{
				Dictionary<string, object> item = new Dictionary<string, object>();
				string value = null;
				value = context.Request.Form["URL"];
				if (value != null) item[Alias.Log.URL] = value;
				value = context.Request.Form["Message"];
				if (value != null) item[Alias.Log.Message] = value;

				result = Add(item);
			}
			else if (operation == "Delete")
            {
                string id = context.Request.QueryString["ID"];

                string[] ids = id.Split('|');
                for (int i = 0; i < ids.Length; i++)
                {
                    if (String.IsNullOrEmpty(ids[i]))
                        continue;

                    Delete(Convert.ToInt32(ids[i]));
                }
            }
            else if (operation == "DeleteAll")
            {
                DeleteAll();
            }
            else if (operation == "List")
            {
                string value = null;

                int page = -1;
                value = context.Request.QueryString["Page"];
                if (value != null) page = Convert.ToInt32(value);

                int pageSize = -1;
                value = context.Request.QueryString["PageSize"];
                if (value != null) pageSize = Convert.ToInt32(value);

                string condition = context.Request.QueryString["Condition"];
                string sort = context.Request.QueryString["Sort"];

                result = List(page, pageSize, condition, sort);
            }
            else
            {
                throw new Exception("lang_header_param");
            }

            return result;
        }

		public int GetInnerIDFromGuid(Guid guid)
        {
            return Dal.GetInnerIDFromGuid(guid);
        }
		public Guid GetGuidFromInnerID(object id)
        {
            return Dal.GetGuidFromInnerID(id);
        }
		public Dictionary<string, object> Get(object id)
        {
            if (CheckRight)
            {
                Guid guid = Dal.GetGuidFromInnerID(id);
                ValidateRight("Get", guid);
            }

            return Dal.Get(id);
        }
		public Guid Add(Dictionary<string, object> item)
        {
            if (CheckRight)
            {
                ValidateRight("Add", Guid.Empty);
            }

            if (!item.ContainsKey(Alias.Log.ID) || item[Alias.Log.ID].ToString() == String.Empty)
                item[Alias.Log.ID] = Guid.NewGuid();
            else if (!(item[Alias.Log.ID] is Guid))
                item[Alias.Log.ID] = new Guid(item[Alias.Log.ID].ToString());

            if (item.ContainsKey(Alias.Log.URL))
            {
                string url = Convert.ToString(item[Alias.Log.URL]);
                if (String.IsNullOrEmpty(url))
                    item[Alias.Log.URL] = DBNull.Value;
				else if (url.Length > 255)
					item[Alias.Log.URL] = url.Substring(0, 255);
			}

            if (item.ContainsKey(Alias.Log.Referrer))
            {
                string referrer = Convert.ToString(item[Alias.Log.Referrer]);
                if (String.IsNullOrEmpty(referrer))
                    item[Alias.Log.Referrer] = DBNull.Value;
				else if (referrer.Length > 255)
					item[Alias.Log.Referrer] = referrer.Substring(0, 255);
			}

			if (!item.ContainsKey(Alias.Log.IP))
				item[Alias.Log.IP] = HttpContext.Current.Request.UserHostAddress;

            Dal.Add(item);

            return (Guid)item[Alias.Log.ID];
        }
		public void Modify(int id, Dictionary<string, object> changes)
		{
			throw new NotImplementedException();
		}
		public void Delete(int id)
        {
            if (CheckRight)
            {
                Guid guid = Dal.GetGuidFromInnerID(id);
                ValidateRight("Delete", guid);
            }

            Dal.Delete(id);
        }
		public void DeleteAll()
        {
            if (CheckRight)
            {
                ValidateRight("DeleteAll", Guid.Empty);
            }

            Dal.DeleteAll();
        }
		public DataTable List(int page, int pageSize, string condition, string sort)
        {
            if (CheckRight)
            {
                ValidateRight("List", Guid.Empty);
            }

            return Dal.List(page, pageSize, condition, sort);
        }
		public int GetRecordCount(string condition)
        {
            return Dal.GetRecordCount(condition);
        }

        #region Properties
		private Dal.ILog Dal
        {
            get { return (Dal.ILog)DalBase; }
        }
        #endregion
    }
}
